import { ButtonEnum, MenuEnum } from '@/lib/enums';

import { useAuth } from '../store/modules/auth';
import { useMemo } from 'react';

// 改成自己的枚举路径
export const useHasButtonAuth = (code) => {
  const { buttonList, userInfo } = useAuth();
  const hasPermission = useMemo(() => {
    return buttonList.some((item) => item.code === code);
  }, [code, buttonList]);
  if (userInfo.isSuperAdmin) return true;
  return hasPermission;
};

export const useButtonPermission = () => {
  const hasUpdatedUserAuthority = useHasButtonAuth(ButtonEnum.UPDATED_USER);
  const hasSetSuperAdminAuthority = useHasButtonAuth(
    ButtonEnum.SET_SUPER_ADMIN,
  );
  const hasEnableUserAuthority = useHasButtonAuth(ButtonEnum.ENABLE_USER);
  const hasDeleteUserAuthority = useHasButtonAuth(ButtonEnum.DELETE_USER);
  const hasDisableUserAuthority = useHasButtonAuth(ButtonEnum.DISABLE_USER);
  const hasRecycleSuperAdminAuthority = useHasButtonAuth(
    ButtonEnum.RECYCLE_SUPER_ADMIN,
  );
  const hasResetPasswordAuthority = useHasButtonAuth(ButtonEnum.RESET_PASSWORD);
  const hasCreateUserAuthority = useHasButtonAuth(ButtonEnum.CREATE_USER);
  const hasRemoveRoleAuthority = useHasButtonAuth(ButtonEnum.REMOVE_ROLE);
  const hasCreateRoleAuthority = useHasButtonAuth(ButtonEnum.CREATE_ROLE);
  const hasRoleAuthorizationAuthority = useHasButtonAuth(
    ButtonEnum.ROLE_AUTHORIZATION,
  );
  const hasUpdateRoleAuthority = useHasButtonAuth(ButtonEnum.UPDATE_ROLE);
  const hasUpdateMenuAuthority = useHasButtonAuth(ButtonEnum.UPDATE_MENU);
  const hasRemoveMenuAuthority = useHasButtonAuth(ButtonEnum.REMOVE_MENU);
  const hasCreateMenuAuthority = useHasButtonAuth(ButtonEnum.CREATE_MENU);
  const hasRemoveSystemAuthority = useHasButtonAuth(ButtonEnum.REMOVE_SYSTEM);
  const hasCreateSystemAuthority = useHasButtonAuth(ButtonEnum.CREATE_SYSTEM);
  const hasUpdateSystemAuthority = useHasButtonAuth(ButtonEnum.UPDATE_SYSTEM);
  return {
    hasUpdatedUserAuthority,
    hasSetSuperAdminAuthority,
    hasEnableUserAuthority,
    hasDeleteUserAuthority,
    hasDisableUserAuthority,
    hasRecycleSuperAdminAuthority,
    hasResetPasswordAuthority,
    hasCreateUserAuthority,
    hasRemoveRoleAuthority,
    hasCreateRoleAuthority,
    hasRoleAuthorizationAuthority,
    hasUpdateRoleAuthority,
    hasUpdateMenuAuthority,
    hasRemoveMenuAuthority,
    hasCreateMenuAuthority,
    hasRemoveSystemAuthority,
    hasCreateSystemAuthority,
    hasUpdateSystemAuthority,
  };
};

export const useMenuPermission = () => {
  const hasUserManage = useHasButtonAuth(MenuEnum.USER_MANAGE);
  const hasRoleManage = useHasButtonAuth(MenuEnum.ROLE_MANAGE);
  const hasAuthManage = useHasButtonAuth(MenuEnum.AUTH_MANAGE);
  return {
    hasUserManage,
    hasRoleManage,
    hasAuthManage,
  };
};
